package com.janlei.acc.factory;

import java.util.HashMap;
import java.util.Map;

/**
 * 工厂模式
 */
public class FactoryStu {
    private static final Map<String, Iterm> map;

    static {
        map = new HashMap(16);
        map.put("玩具", new Toy("大疆air2", 6999));
        map.put("手机", new Phone("三星S10", 6999));
    }

    //获取对象
    public static Iterm getIterm(String kind) {
        if (!map.containsKey(kind)) {
            throw new IllegalArgumentException(String.format("工厂中不存在当在种类：%s", kind));
        }
        return map.get(kind);
    }

    /**
     * 放入iterm到工厂容器中
     *
     * @param name
     * @param iterm
     */
    public static void createIterm(String name, Iterm iterm) {
        map.put(name, iterm);
    }

    /**
     * 移除工厂容器中的iterm
     *
     * @param name
     * @return
     */
    public static boolean removeIterm(String name) {
        map.containsKey(name);
        if (map.containsKey(name)) {
            map.remove(name);
            return true;
        }
        throw new IllegalArgumentException(String.format("容器中没有名为：%s 的Iterm, 无法移除！", name));
    }
}

class Toy extends Iterm {
    public Toy(String name, int price) {
        super(name, price);
    }

    public Toy() {
    }

    @Override
    public String toString() {
        return "Toy{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
}

class Phone extends Iterm {
    public Phone(String name, int price) {
        super(name, price);
    }

    public Phone() {
    }

    @Override
    public String toString() {
        return "Phone{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
}


/**
 * 物品抽象类
 */
abstract class Iterm {
    String name;
    int price;

    public Iterm(String name, int price) {
        this.name = name;
        this.price = price;
    }

    public Iterm() {
    }
}